package com.yunlankeji.stemcells.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.yunlankeji.ganxibaozhijia.R;
import com.yunlankeji.stemcells.activity.home.Home_organizationActivity;
import com.yunlankeji.stemcells.activity.home.Home_organization_classifyActivity;
import com.yunlankeji.stemcells.model.request.Organization_menu;

import java.util.List;

public class Organization_menuAdapter extends RecyclerView.Adapter<Organization_menuAdapter.MyViewholder> implements View.OnClickListener {
    List<Organization_menu>organization_menuList;

    public Organization_menuAdapter(List<Organization_menu> organization_menuList) {
        this.organization_menuList = organization_menuList;
    }

    @NonNull
    @Override
    public MyViewholder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemview = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_organization_menu,parent,false);
        MyViewholder myViewholder = new MyViewholder(itemview);
        return myViewholder;
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewholder holder, int position) {

        Organization_menu organization_menu = organization_menuList.get(position);
        holder.tv_menu.setText(organization_menuList.get(position).getTv_menu());
        holder.iv_menu.setImageResource(organization_menuList.get(position).getIv_menu());


        holder.itemView.setTag(position);
        holder.lt_organization_menu.setTag(position);


    }

    @Override
    public int getItemCount() {
        return organization_menuList.size();
    }

    @Override
    public void onClick(View v) {
        int position = (int) v.getTag();      //getTag()获取数据
        if (mOnItemClickListener != null) {
            switch (v.getId()){
                default:
                    mOnItemClickListener.onItemClick(v, Organization_menuAdapter.ViewName.ITEM, position);
                    getItemId(position);
                    break;
            }
        }
    }

    class MyViewholder extends RecyclerView.ViewHolder {
        TextView tv_menu;
        ImageView iv_menu;
        LinearLayout lt_organization_menu;

        public MyViewholder(View itemView) {
            super(itemView);
            this.tv_menu = itemView.findViewById(R.id.tv_organization_menu);
            this.iv_menu = itemView.findViewById(R.id.iv_organization_menu);
            this.lt_organization_menu = itemView.findViewById(R.id.lt_organization_menu);

            lt_organization_menu.setOnClickListener(Organization_menuAdapter.this);
        }
    }

    public enum ViewName {
        ITEM,
        PRACTISE
    }

    public interface OnItemClickListener  {

        void onItemClick(View v, Organization_menuAdapter.ViewName viewName, int position);
        void onItemLongClick(View v);
    }

    private Organization_menuAdapter.OnItemClickListener mOnItemClickListener;//声明自定义的接口

    //定义方法并传给外面的使用者
    public void setOnItemClickListener(Organization_menuAdapter.OnItemClickListener listener) {
        this.mOnItemClickListener  = listener;
    }

}
